﻿//========================================================= 
/**@file odbc_extractor.cpp
 * @brief ODBC适配资源文件
 * 
 * @date 2018-08-08   16:21:20
 * @author Zhyioo 
 * @version 1.0
 */ 
//--------------------------------------------------------- 
#ifndef _LIBZHOUYB_ODBC_EXTRACTOR_CPP_
#define _LIBZHOUYB_ODBC_EXTRACTOR_CPP_
//--------------------------------------------------------- 
#include "odbc_extractor.h"
//--------------------------------------------------------- 
namespace zhou_yb {
namespace wrapper {
namespace odbc_extractor {
//--------------------------------------------------------- 
sql_dbc sql_env::connect(const char* svr, const char* usr, const char* pwd)
{
    if(!is_valid())
        return sql_dbc();

    SQLHDBC hDbc = SQL_NULL_HDBC;
    SQLRETURN ret = SQLAllocHandle(SQL_HANDLE_DBC, _env, &hDbc);
    if(!SQL_SUCCEEDED(ret))
        return sql_dbc();

    CharConverter svrCvt;
    CharConverter usrCvt;
    CharConverter pwdCvt;
    const char_t* pSvr = svrCvt.to_char_t(svr);
    const char_t* pUsr = svrCvt.to_char_t(usr);
    const char_t* pPwd = svrCvt.to_char_t(pwd);
    ret = SQLConnect(hDbc,
        ctype_cast(SQLTCHAR*)pSvr, SQL_NTS,
        ctype_cast(SQLTCHAR*)pUsr, SQL_NTS,
        ctype_cast(SQLTCHAR*)pPwd, SQL_NTS);

    if(!SQL_SUCCEEDED(ret))
    {
        SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
        return sql_dbc();
    }
    sql_dbc dbc;
    dbc._env = (*this);
    dbc._dbc = hDbc;
    return dbc;
}
sql_stmt sql_dbc::create()
{
    if(!is_valid())
        return sql_stmt();

    SQLHSTMT hStmt = SQL_NULL_HSTMT;
    SQLRETURN ret = SQLAllocHandle(SQL_HANDLE_STMT, _dbc, &hStmt);
    if(!SQL_SUCCEEDED(ret))
        return sql_stmt();

    sql_stmt stmt;
    stmt._dbc = (*this);
    stmt._stmt = hStmt;
    return stmt;
}
//--------------------------------------------------------- 
} // namespace odbc_extractor 
} // namespace wrapper
} // namespace zhou_yb 
//--------------------------------------------------------- 
#endif // _LIBZHOUYB_ODBC_EXTRACTOR_CPP_
//========================================================= 